package com.dekun.workhard.config.security;

import com.dekun.workhard.config.security.filter.MobilePasswordAuthenticationFilter;
import com.dekun.workhard.config.security.handler.CustomerFailHandler;
import com.dekun.workhard.config.security.handler.CustomerSuccessHandler;
import com.dekun.workhard.config.security.service.MobilePasswordDetailService;
import com.dekun.workhard.config.security.service.UsernamePasswordDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * springSecurity配置
 *
 * @author zhoulg
 */
@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UsernamePasswordDetailService usernamePasswordDetailService;
    @Autowired
    private MobilePasswordDetailService mobilePasswordDetailService;
    @Autowired
    private MobilePasswordAuthenticationFilter mobileCodeAuthenticationProcessingFilter;


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/css/**", "/js/**", "/plugins/**", "/images/**", "/fonts/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()
                .loginPage("/login").permitAll()
                .defaultSuccessUrl("/user/index", true)
                .failureUrl("/login_fail")
                .successHandler(customerSuccessHandler())
//                .failureHandler(customerFailHandler())
                .permitAll()
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .logout()
                .logoutSuccessUrl("/logout_success")
                .permitAll()
                .and()
                .csrf().disable()
        ;
        http.addFilterBefore(mobileCodeAuthenticationProcessingFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    public AuthenticationSuccessHandler customerSuccessHandler(){
        return new CustomerSuccessHandler();
    }

    @Bean
    public AuthenticationFailureHandler customerFailHandler(){
        return new CustomerFailHandler();
    }


    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }



    @Bean
    public AuthenticationProvider mobilePasswordProvider(){
        MobilePasswordProvider mobilePasswordProvider = new MobilePasswordProvider();
        mobilePasswordProvider.setPasswordEncoder(passwordEncoder());
        mobilePasswordProvider.setUserDetailsService(mobilePasswordDetailService);
        return mobilePasswordProvider;
    }

    @Bean
    public AuthenticationProvider daoAuthenticationProvider(){
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(usernamePasswordDetailService);
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        return daoAuthenticationProvider;
    }


    /**
     * 注冊provider
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .authenticationProvider(mobilePasswordProvider())
                .authenticationProvider(daoAuthenticationProvider());
    }
}